#include "Admin.h"

Admin::Admin(Database dbInput) : Users( dbInput){

}

void Admin::promptCreateUser(){
	cout << "Creating user.."<<endl<<endl;
	string usernameInput, passwordInput, typeInput, fullNameInput, emailInput;
	cout << "Enter username: ";
	cin >> usernameInput;
	cin.clear();
	cin.sync();
	if (!db.checkForUser(usernameInput)){
		cout << "Enter password: ";
		cin >>  passwordInput;
		cin.clear();
		cin.sync();
		cout << "Enter type: ";
		cin >> typeInput;
		cin.clear();
		cin.sync();
		cin.ignore();
		cout << "Enter full name: ";
		getline(cin, fullNameInput);
		cin.clear();
		cin.sync();
		cout << "Enter Email: ";
		cin >> emailInput;
		cin.clear();
		cin.sync();
		createUser(usernameInput, passwordInput, typeInput, fullNameInput, emailInput);
		if (typeInput == "ADMIN")
			addAdmin(fullNameInput);
		else if (typeInput == "LECT")
			addLecturer(fullNameInput);
		else if (typeInput == "STUDENT")
			addStudent(fullNameInput);
		
	}
	else 
		cout << "The username already exists.." <<endl;
}
void Admin::promptEditUser(){
	string usernameInput, columnInput, newUsername, newInput;
	cout << "Enter username to edit: ";
	cin >> usernameInput;
	cin.clear();
	cin.sync();
	if (db.checkForUser(usernameInput)){ // if there's a existing record
		cout <<endl<< "Editing users ..." <<endl <<endl;
		cout << "Enter the attribute you want to edit(username/password/type/fullname/status/email): " << endl;
		cout << "> ";
		cin >> columnInput;
		cin.clear();
		cin.sync();
		if (columnInput == "username"){ // if the column to change is username
			cout << "Enter new username: ";
			cin >> newUsername;
			cin.clear();
			cin.sync();
			if (!db.checkForUser(newUsername)) // if the new username is not inside records
				editUser(usernameInput, columnInput, newUsername);
			// else there's already a record
			else 
			cout << "There's already such a username!";

		}// endif column to change is username
		else if(columnInput == "type"){
			cout << "To change user type, delete and re-create." << endl <<endl;
		}
		else if(columnInput == "password"){
			bool alphaPresent = false;
			bool numPresent = false;
			cout << "Enter new value for " << columnInput<< ": ";
			cin.ignore();
			getline(cin, newInput);
			cin.clear();
			cin.sync();
			if (newInput.length() < 8){
				cout << "Your password need to be at least characters long."<<endl<<endl;
			}
			else{
				for (int i=0; i<newInput.length();i++){
					if (isalpha(newInput[i]))
						alphaPresent = true;
					if (isdigit(newInput[i]))
						numPresent = true;
				}
				if (alphaPresent == true && numPresent == true)
					editUser(usernameInput, columnInput, newInput);
				else
					cout << "Your password contain at least one number and one alphabet."<<endl <<endl;
			}

		}
		else {
			cout << "Enter new value for " << columnInput<< ": ";
			cin >> newInput;
			cin.clear();
			cin.sync();
			editUser(usernameInput, columnInput, newInput);

		} 
		//endif column to be edit is username
		
	}
	else
	cout << "There's no such user to edit ... "<<endl;
}
void Admin::promptDeleteUser(){
	cout << "Deleting user ..." << endl<<endl;
	string usernameInput;
	cout << "Enter username to delete: ";
	cin >> usernameInput;
	cin.clear();
	cin.sync();
	if (!db.checkForUser(usernameInput))
		cout << "No such user exists ..."<< endl;
	else
		deleteUser(usernameInput);
}
void Admin::promptCreateModule(){
	string moduleInput1, moduleInput2, hoursInput;
	cout << "Creating Module ..." <<endl << endl;
	cout << "Enter module code: ";
	cin >> moduleInput1;
	cin.clear();
	cin.ignore();
	if (!db.checkModuleCode(moduleInput1)){
		cout << "Enter module name: ";
		getline(cin, moduleInput2);
		cin.clear();
		cin.sync();
		cout << "Enter total teaching hours: ";
		cin >> hoursInput;
		cin.clear();
		cin.sync();
		createModule(moduleInput1, moduleInput2, hoursInput);
	}
	else
	cout << "There's already such a module ...";
					
}
void Admin::promptEditModule(){
	string moduleInput1, moduleInput2, moduleInput3;
	cout << "Editing a module ..." <<endl<<endl;
	cout << "Enter module code to be edited: ";
	cin >> moduleInput1;
	cin.clear();
	cin.sync();
	if (db.checkModuleCode(moduleInput1)){ //  if there is such a record to edit
		cout << "Enter the attribute to edit(module_code/module_name/total_hours): ";
		cin >> moduleInput2;
		cin.clear();
		cin.sync();
		if (moduleInput2 == "module_code"){ // if the attribute to be edited is module code
			cout << "Enter new module_code: ";
			cin >> moduleInput3;
			cin.clear();
			cin.sync();
			if (!db.checkModuleCode(moduleInput3))
			 // if the new module_code doesn't clash with other existing module_code
				editModule(moduleInput1, moduleInput2, moduleInput3);
			else
				cout << "There's already a existing module code ... " <<endl;
		}// end of if the attribute to edit is module_code
		else {
			cin.ignore();
			cout << "Enter new module_name: ";
			getline(cin, moduleInput3);
			cin.clear();
			cin.sync();
			editModule(moduleInput1, moduleInput2, moduleInput3);
		}
		
	} // end of if there's such a record to edit
	else
		cout <<"There no such module ... "<< endl;
	
}
void Admin::promptDeleteModule(){
	string moduleInput1;
	cout << "Deleting Module ..." << endl<<endl;
	cout << "Enter a module code to be deleted: ";
	cin >> moduleInput1;
	cin.clear();
	cin.sync();
	if (db.checkModuleCode(moduleInput1) == FOUND)
	deleteModule(moduleInput1);
	if (db.checkModuleCode(moduleInput1) != FOUND)
	cout << "Module has been successfully deleted!"<< endl<<endl;
	
					
}

void Admin::promptCreateLesson(){
	cout << "Creating lesson ..." <<endl<<endl;
	string moduleCodeInput, lessonIdInput, timeslotInput, venueInput;
	cout << "Please enter module code of the lesson: ";
	cin >> moduleCodeInput;
	cin.clear();
	cin.sync();
	if (db.checkModuleCode(moduleCodeInput)== true){ // if there's such a module
		cin.ignore();
		cout << "Please enter lesson id: ";
		getline(cin, lessonIdInput);
		cin.clear();
		cin.sync();
		if (db.checkForLessonsId(lessonIdInput) != true){ // if there's no such lesson
			cout << "Please enter venue: ";
			getline(cin, venueInput);
			cin.clear();
			cin.sync();
			if (db.checkForVenue(venueInput) ==  true){ // if the venue exists
				cout << "Please enter timeslot (YYYY-mm-dd HH:MM): ";
				getline(cin, timeslotInput);
				
				cin.clear();
				cin.sync();
				if (db.checkForTimeslot(timeslotInput) == true){ // if timeslot exists
					vector<vector<string> > ptId = db.returnPtId(venueInput, timeslotInput);
					
					if (ptId.size() == 0) // if there's no venue and timeslot combination
						cout << "error! no such timeslot" <<endl;
					else // everything is okay
					createLesson(moduleCodeInput, lessonIdInput, ptId[0][0]);
					
					
				}// end if timeslot exists
				else
					cout << "There's no such timeslot" << endl;
			}// endif venue exist
			else
				cout << "There's no such venue" << endl;
		}// endif lesson not exists
		else
		cout << "There's already a lesson with that lesson id" << endl;
	}// endif there's such a module
	else
	cout << "There's no such module." << endl;
}

void Admin::promptEditLesson(){
	cout <<"Editing lesson ..." <<endl<<endl;
	string lessonIdInput, columnInput, newInput;
	cout << "Please enter the Lesson to be edit: ";

	getline(cin, lessonIdInput);
	
	cin.clear();
	cin.sync();
	if (db.checkForLessonsId(lessonIdInput) == FOUND){
		cout << "Please enter the attribute to edit (lessons_id/module_code/pt_id/notice): ";
		cin >> columnInput;
		cin.clear();
		cin.ignore();
		
		cout << "Please enter the new attribute: ";

		if (columnInput == "lessons_id"){
			getline(cin, newInput);
			
			if (db.checkForLessonsId(newInput) == IS_EMPTY)
				editLesson(lessonIdInput, columnInput, newInput);
			else
				cout << "There's already such a lesson id." << endl;
		} 
		else {
			getline(cin, newInput);
			
			editLesson(lessonIdInput, columnInput, newInput);
		}
		cin.clear();
		cin.sync();

	}// endif checkForLessonId is empty
	else
		cout << "There's no such lesson." <<endl <<endl;
}

void Admin::promptDeleteLesson(){
	string lessonIdInput;
	cout <<"Deleting Lesson ..." <<endl<<endl;
	cout << "Please enter the lesson id to be delete: ";
	getline(cin,lessonIdInput);
	cin.clear();
	cin.sync();
	if (db.checkForLessonsId(lessonIdInput)==FOUND)
		deleteLesson(lessonIdInput);
	
}

void Admin::promptAssignLecturerToModule(){
	string fullNameInput, moduleCodeInput;
	cout <<"Assigning Lecturer to Module ..." <<endl<<endl;
	cout <<"Please enter lecturer's name: ";
	getline(cin, fullNameInput);
	cin.clear();
	cin.sync();
	if(db.checkForLecturer(fullNameInput) == FOUND){
		cout << "Please enter module code: ";
		cin>>moduleCodeInput;
		cin.clear();
		cin.sync();
		if (db.checkModuleCode(moduleCodeInput)==FOUND){
			assignLecturerToModule(fullNameInput, moduleCodeInput);
			cout << "successfully assigned lecturer to module." << endl <<endl;
		}
		else
			cout << "No such module is found"<< endl;
	}
	else
		cout << "No such lecturer is found"<< endl;
}


void Admin::promptAssignLecturerToLesson(){
	string fullNameInput, lessonIdInput;
	cout << "Assigning lecturer to lessons ..."<<endl<<endl;
	cout << "Please enter lecturer's name: ";
	
	getline(cin, fullNameInput);
	cin.clear();
	cin.sync();
	if (db.checkForLecturer(fullNameInput) == FOUND){
		cout << "Please enter lesson id: ";
		getline(cin, lessonIdInput);
		cin.clear();
		cin.sync();
		if (db.checkForLessonsId(lessonIdInput) == FOUND){
			vector<vector<string> >moduleOfLesson = db.returnModuleOfLesson(lessonIdInput);
			vector<vector<string> > listOfModules = db.returnModuleFromLM(fullNameInput);
			bool lecturerTeachesModule = false;
			
			for (int i=0; i< listOfModules.size(); i++){
				if (moduleOfLesson[0][0] == listOfModules[i][0])
					lecturerTeachesModule = true;
			}
			if (lecturerTeachesModule == true)
				assignLecturerToLesson(fullNameInput, lessonIdInput);
				cout << "Successfully assigned lecturer to Lesson.";
		}
		else
			cout <<"No such lesson is found."<< endl;
	}
	else
		cout <<"No such Lecturer is found." << endl;
}


void Admin::createUser(string usernameInput, string passwordInput, string typeInput, string fullNameInput, string emailInput){
	db.query("INSERT INTO `USERS` (username, password, type, status, fullname, email, tries) VALUES ('"+usernameInput+"','"+passwordInput+"','"+typeInput+"','OK','"+fullNameInput+"','"+emailInput+"', 0);");
	if (db.checkForUser(usernameInput) == FOUND)
		cout << "User successfully added!" <<endl <<endl;
}

void Admin::editUser(string usernameInput, string columnInput, string editInput){
	db.query("UPDATE `USERS` SET `"+columnInput+"` = '"+editInput+"' WHERE username = '"+usernameInput+"';");
}

void Admin::deleteUser(string usernameInput){
	db.query("DELETE FROM USERS WHERE username = '"+ usernameInput +"';");
	if (db.checkForUser(usernameInput) != FOUND)
		cout << "User successfully deleted!" <<endl <<endl;

}
void Admin::addAdmin(string fullNameInput){
	db.query("INSERT INTO ADMINS (fullname) VALUES ('"+fullNameInput+"');");
}
void Admin::addLecturer(string fullNameInput){
	db.query("INSERT INTO LECTURERS (fullname) VALUES ('"+fullNameInput+"');");

}
void Admin::addStudent(string fullNameInput){
	db.query("INSERT INTO STUDENTS (fullname) VALUES ('"+fullNameInput+"');");

}


void Admin::createModule(string moduleCodeInput, string moduleNameInput, string hoursInput){
	db.query("INSERT INTO MODULES (module_code, module_name, total_hours) VALUES ('"+ moduleCodeInput +"','"+ moduleNameInput +"', "+hoursInput+");");
	if (db.checkModuleCode(moduleCodeInput)== FOUND)
		cout << "Module has been added successfully!" << endl <<endl;
}

void Admin::editModule(string moduleCodeInput, string columnInput, string editInput){
	db.query("UPDATE MODULES SET "+columnInput+" = '"+ editInput+ "' WHERE module_code = '"+ moduleCodeInput  + "';");
}

void Admin::deleteModule(string moduleCodeInput){
	db.query("DELETE FROM MODULES WHERE module_code = '"+ moduleCodeInput +"';");
	if(db.checkModuleCode(moduleCodeInput)!= FOUND)
		cout << "Module has been deleted successfully!"<< endl << endl;
}

void Admin::createLesson(string moduleCodeInput, string lessonIdInput, string ptIdInput){
	db.query("INSERT INTO LESSONS (lessons_id, module_code, pt_id, notice) values ('"+lessonIdInput+"', '"+moduleCodeInput+"', "+ptIdInput+", '');");
	if (db.checkForLessonsId(lessonIdInput) == FOUND)
		cout << "Lesson has been successfully added!" <<endl<<endl;
	
}


void Admin::editLesson(string lessonIdInput, string columnInput, string newInput){
	db.query("UPDATE LESSONS SET `"+columnInput+"` = '"+newInput+"' WHERE lessons_id = '"+lessonIdInput+"';");
}

void Admin::deleteLesson(string lessonIdInput){
	db.query("DELETE FROM LESSONS WHERE lessons_id = '"+lessonIdInput+"';");
	if(db.checkForLessonsId(lessonIdInput)!=FOUND)
		cout << "Lesson has been successfully deleted.." << endl<< endl;
}


void Admin::assignLecturerToModule(string fullNameInput, string moduleCodeInput){
	db.query("INSERT INTO LECTURERS_MODULES (lm_id, fullname, module_code) values (null, '"+fullNameInput+"', '"+moduleCodeInput+"');");
}

void Admin::assignLecturerToLesson(string fullNameInput, string lessonIdInput){
	db.query("INSERT INTO LECTURERS_LESSONS(ll_id, fullname, lessons_id) values (null, '"+fullNameInput+"', '"+lessonIdInput+"');");
}

void Admin::showListOfLessons(){
	vector<vector<string> > listOfLessons = db.query("SELECT lessons_id FROM LESSONS;");
	for (int i=0;i<listOfLessons.size();i++)
		cout << listOfLessons[i][0] << endl;
}

void Admin::showListOfLecturer(){
	vector<vector<string> > listOfLecturers = db.query("SELECT fullname FROM LECTURERS;");
	for(int i=0;i<listOfLecturers.size();i++)
		cout << listOfLecturers[i][0]<<endl;
	
}

void Admin::adminMenu(){
	int optionSelect;
	int editSelect;
	bool badSelection;
	
	cout <<endl<< "You're Logged in as administrator." <<endl<<endl;
	cout << "List of Lessons" <<endl<<endl;
	showListOfLessons();
	cout << endl <<endl;
	cout << "List of Lecturers" << endl<<endl;
	showListOfLecturer();
	cout << endl<<endl;

	do{
		badSelection = false;

		cout <<endl << endl << "My Menu" << endl 
			 << "===========================" << endl << endl;
		cout << "1. Create User"<<endl;
		cout << "2. Edit User"<<endl;
		cout << "3. Delete User"<<endl;
		cout << "4. Create Module" << endl;
		cout << "5. Edit Module"<< endl;
		cout << "6. Delete Module"<< endl;
		cout << "7. Create Module lesson" <<endl;
		cout << "8. Edit Module lesson" <<endl;
		cout << "9. Delete Module lesson" <<endl;
		cout << "10. Assign Lecturer to module" <<endl;
		cout << "11. Assign Lecturer to lesson" <<endl;
		cout << "12. Quit" <<endl;


		cout <<endl<< "Please Enter your selection: ";
		cin >> optionSelect;
		cin.clear();
		cin.ignore(100, '\n');
		

		switch(optionSelect){
			case 1: promptCreateUser();
					break;
			case 2: promptEditUser();
					break;
			case 3: promptDeleteUser();
					break;
			case 4: promptCreateModule();
					break;
			case 5: promptEditModule();
					break;
			case 6: promptDeleteModule();
					break;
			case 7: promptCreateLesson();
					break;
			case 8: promptEditLesson();
					break;
			case 9: promptDeleteLesson();
					break;
			case 10: promptAssignLecturerToModule();
					break;
			case 11: promptAssignLecturerToLesson();
					break;
			case 12 : exit(-1);
					break;
			default: badSelection = true;
					break;
		}
	}while(badSelection == true || optionSelect != 12);
}


